package com.iocaop.uaa.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
// 多配置 需要设定顺序  不能是一样的  页面登录
@Order(100)

public class LoginSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     *
     * @param auth
     * @throws Exception
     */
    @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .passwordEncoder(passwordEncoder)
            .withUser("admin").password("{bcrypt}$2a$10$2CJGv23GOk7WMXmGhmN95OwI8TwooAEVQY82FNUgsti.ojjpXlAXm").roles("ADMIN")
            .and().withUser("lzc").password("{SHA-1}{oftkwH/HMIA+w7beZJ3lBxOE3CSnZCbZXrXdU+De/pM=}e885b986a1aac96a2c56358e03ec9ae864d3776a").roles("ADMIN");

    }

    @Override protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
            .anyRequest().authenticated()
            // 指定登录页，配置访问权限
            .and().formLogin().loginPage("/login")
            // 配置登录成功以后跳转的页面
            .defaultSuccessUrl("/index")
            .permitAll()
            .and().logout().logoutUrl("/perform_logout").permitAll()
            // 设置token有效时间 和 名字
            .and().rememberMe().tokenValiditySeconds(30*24*3600).rememberMeCookieName("someKey");
    }

    @Override public void configure(WebSecurity web) throws Exception {
        web.ignoring().mvcMatchers("/public/**","/error");
    }
}

